import pandas as pd
from my_init import fuc_MD
import threading
from basic_params import Basic_var, main_start_Que
from data_process import func_store, func_sort, pack_and_upload
from updateinstr import UpdateInstrs_after_store


def get_daily_channel():
    '''
    init_prepare确认交易日后在main_que放入今日数据通道que,随后创建新线程实现数据sort_store_pack_upload,直到下一个交易日该线程阻塞
    '''
    while True:
        receive_data_Que = main_start_Que.get()
        threading.Thread(target=sort_store_pack_upload,
                         args=[receive_data_Que]).start()


def sort_store_pack_upload(receive_data_Que):
    '''
    创建大字典 key为instrid value为数据容器dataframe
    当从receive_data_Que收到非false数据后执行func_sort
    收到false执行func_store/UpdateInstrs_after_store,创建线程执行pack_and_upload
    '''

    df_dic = {}
    instrid_l = [x.split('.')[-1]
                 for x in Basic_var.excg_instrID_list]
    for i in instrid_l:
        df_dic[i] = pd.DataFrame()
    while True:
        data = receive_data_Que.get()
        if not data:  # 收到False后结束
            func_store(df_dic)
            UpdateInstrs_after_store(df_dic)
            threading.Thread(target=pack_and_upload).start()
            return
        func_sort(data, df_dic)


threading.Thread(target=fuc_MD).start()
threading.Thread(target=get_daily_channel).start()
